package com.carkeeper.user.service.impl;

import com.carkeeper.pojo.Admin;
import com.carkeeper.pojo.Department;
import com.carkeeper.pojo.Employee;
import com.carkeeper.user.dao.AdminMapper;
import com.carkeeper.user.service.AdminService;
import com.config.MD5_SHA_1_Utils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public List<Admin> findAll() {

        return adminMapper.selectAll();
    }

    @Override
    public Page<Admin> findPage(Map searchMap, int page, int size) {
        PageHelper.startPage(page,size);
        Example example = createExample(searchMap);
        return (Page<Admin>) adminMapper.selectByExample(example);
    }

    @Override
    public void add(Admin admin) {

        String password = admin.getPassword();

        String gensalt = BCrypt.gensalt();

        String password1 = BCrypt.hashpw(password, gensalt);

        admin.setPassword(password1);

        adminMapper.insertSelective(admin);
    }

    @Override
    public void update(Admin admin) {
        adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public Admin lockBlock(Integer id) {

        Admin a = new Admin();
        a.setId(id);

        Admin admin = adminMapper.selectByPrimaryKey(a);

        if (admin == null){

                throw new RuntimeException("id不存在");
            }
            if (admin.getAccountStatus() != 1){

                admin.setAccountStatus(1);


            }

            return admin;
    }

    @Override
    public Admin findById(Integer id) {
        return adminMapper.selectByPrimaryKey(id);


    }

    @Override
    public Admin findByUsername(String username) {

        Example example = new Example(Admin.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("username",username);
        return adminMapper.selectOneByExample(example);
    }


    private Example createExample(Map<String, Object> searchMap) {
        Example example = new Example(Department.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 分类名称
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 是否显示
            if (searchMap.get("isShow") != null && !"".equals(searchMap.get("isShow"))) {
                criteria.andEqualTo("isShow", searchMap.get("isShow"));
            }
            // 是否导航
            if (searchMap.get("isMenu") != null && !"".equals(searchMap.get("isMenu"))) {
                criteria.andLike("isMenu", "%" + searchMap.get("isMenu") + "%");
            }

            // 分类ID
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
            // 商品数量
            if (searchMap.get("goodsNum") != null) {
                criteria.andEqualTo("goodsNum", searchMap.get("goodsNum"));
            }
            // 排序
            if (searchMap.get("seq") != null) {
                criteria.andEqualTo("seq", searchMap.get("seq"));
            }
            // 上级ID
            if (searchMap.get("parentId") != null) {
                criteria.andEqualTo("parentId", searchMap.get("parentId"));
            }
            // 模板ID
            if (searchMap.get("templateId") != null) {
                criteria.andEqualTo("templateId", searchMap.get("templateId"));
            }

        }
        return example;
    }
}
